package com.fjl.basic;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapTest
{
	public static void main(String[] args)
	{
		hashMapTest();
	}

	/**
	 * HashMap test
	 */
	public static void hashMapTest()
	{
	   Map<String,Long> hashMaps = new HashMap<String,Long>();
	   hashMaps.put("1", (long) 22);
	   hashMaps.put("1", (long) 23);
	   hashMaps.put("1", (long) 24);
	   hashMaps.put("2", (long) 2);
	   hashMaps.put("3", (long) 3);
	   System.out.println(hashMaps.get("1"));
	   hashMaps.remove("3");
	   firstMethod(hashMaps);
	   
	   secondMethod2(hashMaps);
	   
	}
	
	/**
	 * 遍历hashmap测试
	 * @param map
	 */
	private static void firstMethod(Map<String,Long> map) {
        long startTime = System.currentTimeMillis();
        System.out.println("第一种方式：通过遍历Map.keySet()遍历HashMap的key和value");
        for (String key : map.keySet()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }
        System.out.println("第一种耗时:"+(System.currentTimeMillis() - startTime));
    }


    private static void secondMethod1(Map<String, String> map) {
        long startTime = System.currentTimeMillis();
        startTime = System.currentTimeMillis();
        System.out.println("第二种方式：通过遍历values()遍历Map的value，但是不能遍历key");
        Collection<String> values = map.values();
        for(Iterator<String> it2 = values.iterator();it2.hasNext();){
            it2.next();
        }

        System.out.println("第二种耗时:"+(System.currentTimeMillis() - startTime));
    }

    private static void secondMethod2(Map<String,Long> map) {
        long startTime = System.currentTimeMillis();
        startTime = System.currentTimeMillis();
        System.out.println("第二种方式：通过遍历values()遍历Map的value，但是不能遍历key");
        for (Long v : map.values()) {
            System.out.println("value= " + v);
        }
        System.out.println("第二种耗时:"+(System.currentTimeMillis() - startTime));
    }
}
